package com.heima;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DiamondShape implements DrawableShape {
    double centerX, centerY, width = 100, height = 60;
    Color color;
    String text;

    DiamondShape(double centerX, double centerY, Color color, String text) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.color = color;
        this.text = text;
    }

    @Override
    public void draw(GraphicsContext gc) {
        gc.setFill(color);
        // 绘制菱形
        gc.fillPolygon(new double[]{centerX, centerX + width/2, centerX, centerX - width/2}, 
                       new double[]{centerY - height/2, centerY, centerY + height/2, centerY}, 4);
        // 设置固定边框粗细为1.0，避免受连接线笔触影响
        gc.setLineWidth(1.0);
        gc.setStroke(color);
        gc.strokePolygon(new double[]{centerX, centerX + width/2, centerX, centerX - width/2}, 
                        new double[]{centerY - height/2, centerY, centerY + height/2, centerY}, 4);
        gc.setFill(Color.BLACK);
        gc.fillText(text, centerX, centerY);
    }

    @Override
    public boolean contains(double mouseX, double mouseY) {
        // 简单的菱形检测
        double relX = Math.abs(mouseX - centerX);
        double relY = Math.abs(mouseY - centerY);
        return (relX / (width/2) + relY / (height/2)) <= 1;
    }

    @Override
    public void setColor(Color color) {
        this.color = color;
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    @Override
    public Color getColor() {
        return color;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public void move(double deltaX, double deltaY) {
        centerX += deltaX;
        centerY += deltaY;
    }

    @Override
    public DrawableShape copy() {
        return new DiamondShape(centerX, centerY, color, text);
    }
    
    @Override
    public Element toXml(Document doc) {
        Element element = doc.createElement("shape");
        element.setAttribute("type", "diamond");
        element.setAttribute("centerX", String.valueOf(centerX));
        element.setAttribute("centerY", String.valueOf(centerY));
        element.setAttribute("width", String.valueOf(width));
        element.setAttribute("height", String.valueOf(height));
        element.setAttribute("color", color.toString());
        element.setAttribute("text", text);
        
        return element;
    }
    
    // 新增方法：绘制连接点
    @Override
    public void drawConnectionPoints(GraphicsContext gc) {
        // 绘制菱形图形的四个连接点（上下左右）
        gc.setFill(Color.BLUE);
        
        // 上部连接点
        gc.fillRect(centerX - 5, centerY - height/2 - 5, 10, 10);
        // 下部连接点
        gc.fillRect(centerX - 5, centerY + height/2 - 5, 10, 10);
        // 左侧连接点
        gc.fillRect(centerX - width/2 - 5, centerY - 5, 10, 10);
        // 右侧连接点
        gc.fillRect(centerX + width/2 - 5, centerY - 5, 10, 10);
    }
    
    // 新增方法：获取选中的连接点
    @Override
    public ConnectionPoint getSelectedConnectionPoint(double mouseX, double mouseY) {
        // 检查鼠标是否点击在某个连接点上
        double[][] points = {
            {centerX, centerY - height/2},     // 上
            {centerX, centerY + height/2},     // 下
            {centerX - width/2, centerY},       // 左
            {centerX + width/2, centerY}        // 右
        };
        
        for (int i = 0; i < points.length; i++) {
            if (Math.abs(mouseX - points[i][0]) <= 10 && Math.abs(mouseY - points[i][1]) <= 10) {
                String direction;
                switch (i) {
                    case 0: direction = "top"; break;
                    case 1: direction = "bottom"; break;
                    case 2: direction = "left"; break;
                    default: direction = "right";
                }
                
                // 计算连接点坐标
                double pointX, pointY;
                
                switch (direction) {
                    case "top":
                        pointX = centerX;
                        pointY = centerY - height / 2;
                        break;
                    case "bottom":
                        pointX = centerX;
                        pointY = centerY + height / 2;
                        break;
                    case "left":
                        pointX = centerX - width / 2;
                        pointY = centerY;
                        break;
                    case "right":
                        pointX = centerX + width / 2;
                        pointY = centerY;
                        break;
                    default:
                        return null;
                }
                
                // 使用正确参数调用 ConnectionPoint 构造方法
                return new ConnectionPoint(pointX, pointY, direction, this);
            }
        }
        
        return null;
    }
}
